/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.renderer;

import com.jb.web.bean.WebTreeNode;
import com.jb.web.context.WebContext;

import java.util.Iterator;

import com.jb.faces.component.UIComponent;


public class SideMenuRenderer extends MenuItemRenderer {
    public StringBuffer render(WebContext pCtx, UIComponent item) {
        return renderBegin(pCtx, item).append(renderChildren(pCtx, item)).append(renderEnd(pCtx, item));
    }

    public StringBuffer renderBegin(WebContext pCtx, UIComponent itemToRender) {
        WebTreeNode mi = (WebTreeNode) itemToRender;

        return new StringBuffer().append("<div ").append(addTagProperty("style", mi.getStyle(pCtx), null))
                                 .append(addTagProperty("class", mi.getStyleClass(), null))
                                 .append(getEditScript(pCtx, mi)).append(">");
    }

    public StringBuffer renderChildren(WebContext pCtx, UIComponent item) {
        WebTreeNode mi = (WebTreeNode) item;
        StringBuffer stringBuffer = new StringBuffer();

        if ((mi.getChildCount() > 0) && (mi.isRoot() || mi.isOpen())) {
            for (Iterator i = mi.getChildNodes().iterator(); i.hasNext();) {
                stringBuffer.append(renderCell(pCtx, (WebTreeNode) i.next()));
            }
        }

        return stringBuffer;
    }

    public StringBuffer renderEnd(WebContext pCtx, UIComponent itemToRender) {
        return new StringBuffer().append("</div>");
    }

    private StringBuffer renderCell(WebContext pCtx, WebTreeNode mi) {
        StringBuffer stringBuffer = new StringBuffer();

        String dest = (String) evaluate(pCtx, mi, mi.getDestination());
        dest = pCtx.formURL(dest, mi) + "&action=select";
        stringBuffer.append("<div >&nbsp;");
        stringBuffer.append("<span ").append(addTagProperty("onClick", "JBUtil.gotoUrl('" + dest + "')", null));

        String styleClass = mi.isSelected() ? mi.getSelectedStyle(pCtx) : mi.getStyleClass();
        stringBuffer.append(addTagProperty("style", mi.getStyle(pCtx), null))
                    .append(addTagProperty("class", styleClass, null)).append(">");

        String label = (String) evaluate(pCtx, mi, mi.getValue());

        if (isEmptyString(label)) {
            label = mi.getId();
        }

        stringBuffer.append(label);

        stringBuffer.append("</span></div>");

        return stringBuffer.append(renderChildren(pCtx, mi));
    }
}
